package com.hook3.pianke.activity.base;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hook3.pianke.Net.Url;
import com.hook3.pianke.R;
import com.hook3.pianke.Utiles.Utiles;
import com.hook3.pianke.activity.InfoActivity;
import com.hook3.pianke.domain.TopicInfoDetail;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.IOException;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;


/**
 * Created by HOME on 2015/12/10.
 */
public class BaseActiviy extends AppCompatActivity {
    private static final String TAG = BaseActiviy.class.getSimpleName();
    public Toolbar toolbar;  //标题
    private FrameLayout frameLayout;//实现View;
    private TopicInfoDetail infoDetail;
    private RelativeLayout article_actionbar_comment_view;
    private ImageView article_actionbar_comment_iv;
    private TextView article_actionbar_comment_count_tx;
    private RelativeLayout article_actionbar_like_view;
    private ImageView article_actionbar_like_img;
    private TextView article_actionbar_like_count_tx;
    private RelativeLayout article_actionbar_font_view;
    private RelativeLayout article_actionbar_more_btn;
    private String contentid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_bar_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        x.Ext.init(getApplication());  //xutils工具初始化
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.abc_ic_ab_back_mtrl_am_alpha);
        toolbar.setNavigationOnClickListener(new MyToolbarOnClickListener());
        frameLayout = (FrameLayout) findViewById(R.id.content_layout);
        contentid = getIntent().getStringExtra("contentid");  //传进来的标识
        initView();  //加载数据
    }

    private void initToolbar() {
        //初始化工具栏
        View inflate = View.inflate(this, R.layout.actionbar_article_info, null);
        toolbar.addView(inflate);
        int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        inflate.measure(width, height);
        int i = toolbar.getWidth() - toolbar.getChildAt(0).getWidth() - inflate.getMeasuredWidth();
        inflate.setPadding(i, 0, 0, 0);
        article_actionbar_comment_view = (RelativeLayout) inflate.findViewById(R.id.article_actionbar_comment_view);
        article_actionbar_comment_iv = (ImageView) inflate.findViewById(R.id.article_actionbar_comment_iv);
        article_actionbar_comment_count_tx = (TextView) inflate.findViewById(R.id.article_actionbar_comment_count_tx);
        article_actionbar_like_view = (RelativeLayout) inflate.findViewById(R.id.article_actionbar_like_view);
        article_actionbar_like_img = (ImageView) inflate.findViewById(R.id.article_actionbar_like_img);
        article_actionbar_like_count_tx = (TextView) inflate.findViewById(R.id.article_actionbar_like_count_tx);
        article_actionbar_font_view = (RelativeLayout) inflate.findViewById(R.id.article_actionbar_font_view);
        article_actionbar_more_btn = (RelativeLayout) inflate.findViewById(R.id.article_actionbar_more_btn);
        int commenttotal = infoDetail.getCommenttotal();
        int like = infoDetail.getPostsinfo().getCounterList().getLike();
        if (commenttotal > 0) {
            article_actionbar_comment_count_tx.setVisibility(View.VISIBLE);
            article_actionbar_comment_count_tx.setText(infoDetail.getCommenttotal() + "");
        }
        //article_actionbar_like_count_tx
        if (like > 0) {
            article_actionbar_like_count_tx.setVisibility(View.VISIBLE);
            article_actionbar_like_count_tx.setText(like + "");
        }
        //我的页面不需要字体
        article_actionbar_font_view.setVisibility(View.GONE);
    }


    private GifDrawable gifDrawable;

    protected void initView() {
        final View inflate = View.inflate(this, R.layout.include_loading, null);
        // 从xml中得到GifView的句柄
        GifImageView gifImageView = (GifImageView) inflate.findViewById(R.id.loading_img);
        try {
            gifDrawable = new GifDrawable(getAssets(), "loading.gif");
            //gifDrawable.setCornerRadius(1.5f);
            gifDrawable.setSpeed(1.5f);
            gifImageView.setImageDrawable(gifDrawable);
        } catch (IOException e) {
            e.printStackTrace();
        }
        gifDrawable.start();
        frameLayout.addView(inflate);
        //开始网络解析数据
        RequestParams params = new RequestParams(Url.shopinfo_url);
        params.addBodyParameter("contentid", contentid);
        params.addBodyParameter("client", "2");  //顾客  猜测应该是用来传递登陆状态的
        x.http().post(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                Log.e(TAG, result);
                //拿到数据解析数据
                Gson gson = new Gson();
                infoDetail = gson.fromJson(Utiles.Json(result).toString(), TopicInfoDetail.class);
                Log.e(TAG, infoDetail.getCommenttotal() + "");
                //初始化工具栏
                initToolbar();
                initInfo();
                frameLayout.removeView(inflate);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    /**
     * 搞定详情页面
     */
    private void initInfo() {

        new InfoActivity(this, toolbar, frameLayout, infoDetail);
    }

    class MyToolbarOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            BaseActiviy.this.finish();
        }
    }

}
